//
//  FunPhotoTableViewCell.m
//  USB_Reader
//
//  Created by xiaozao on 15/3/24.
//  Copyright (c) 2015年 Tony_Wang. All rights reserved.
//

#import "FunPhotoTableViewCell.h"
#import "UIImageView+WebCache.h"
@implementation FunPhotoTableViewCell

- (void)dealloc
{
    [_content release];
    [_funImageView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.funView = [[UIView alloc] init];
        [self.contentView addSubview:_funView];
        [_funView release];
        
        self.funImageView = [[UIImageView alloc] init];
        [self.funView addSubview:_funImageView];
        [_funImageView release];
        
        self.content = [[UILabel alloc] init];
        [self.funView addSubview:_content];
        [_content release];
        
        
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.funView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height);
    
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
    CGRect rect = [self.data.content boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width , self.contentView.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    self.content.frame = CGRectMake(0, 0, self.contentView.frame.size.width, rect.size.height);
    self.content.text = self.data.content;
    self.content.numberOfLines = 0;
    [self.content sizeToFit];
    self.content.frame = CGRectMake(0, 0, self.contentView.frame.size.width, rect.size.height);
    
//    NSLog(@"文字高度: %lf", rect.size.height);
//    NSLog(@"文字内容: %@", self.data.content);
    
//    [self.funView addSubview:_content];
    
    NSMutableDictionary *funDic = self.data.image_size;
    NSMutableArray *array = [funDic objectForKey:@"m"];
    CGFloat width = [[array objectAtIndex:0] floatValue];
    CGFloat heigh = [[array objectAtIndex:1] floatValue];
    
    if (width == 0 && heigh == 0) {
        width = 1;
        heigh = 1;
        
    } else {
    CGFloat factWidthWithWidth = self.contentView.frame.size.width  / width;
    CGFloat factHeigh = heigh * factWidthWithWidth;
    
    self.funImageView.frame = CGRectMake(0, rect.size.height + 10, self.contentView.frame.size.width, factHeigh);
//        NSLog(@"图片高度: %lf", self.funImageView.frame.size.height);
        
        NSString *str = self.data.image;

        NSString *firstStr = [str substringWithRange:NSMakeRange(3, 5)];
        NSString *secondStr = [str substringWithRange:NSMakeRange(3, 9)];
        NSString *url = [NSString stringWithFormat:@"http://pic.qiushibaike.com/system/pictures/%@/%@/medium/%@", firstStr, secondStr, str];
        
        NSURL *urlStr = [NSURL URLWithString:url];
        [self.funImageView setImageWithURL:urlStr placeholderImage:[UIImage imageNamed:@"placeholderImage.png"]];

    }
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
